package cn.cas.mango.util.ResponseUtil;

import cn.cas.mango.dto.Material;
import cn.cas.mango.dto.News;
import cn.cas.mango.service.ConfigService;
import cn.cas.mango.util.string.StringUtils;
import co.elastic.clients.elasticsearch.core.search.Hit;
import com.alibaba.fastjson.JSONObject;
import org.apache.logging.log4j.util.Strings;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class ResponseUtil {
  /**
   * 传输id news_author news_website news_publictime_date media_url news_type 字段
   * @param config, 参数实例
   * @param link, 返回的Json
   * @param hit, 原始的新闻
   */
  public static void putCommon(ConfigService config, JSONObject link, Hit<News> hit) {
    News hitSource = hit.source();
    putCommon(link, hitSource);
    List<String> mediaUrlList = new ArrayList<>();
    if (hitSource.getNewsMediaName() != null) {
      if (Strings.isNotBlank(hitSource.getNewsType())) {

        List<String> mediaNames = new ArrayList<>();
        if (hitSource.getNewsMediaName() instanceof String) {
          mediaNames.add((String) hitSource.getNewsMediaName());
        } else {
          mediaNames = (List<String>) hitSource.getNewsMediaName();
        }

        // change
//        List<String> mediaNames = (List<String>) hitSource.getNewsMediaName();
        for (String name : mediaNames) {
          if (hitSource.getNewsType().equalsIgnoreCase("image")) {
            String url = config.getMediaUrl() + "/" + hit.index() + "/image/" + name;
            mediaUrlList.add(url);
          } else if (hitSource.getNewsType().equalsIgnoreCase("video")) {
            String url = config.getMediaUrl() + "/" + hit.index() + "/video/" + name;
            mediaUrlList.add(url);
          }
        }
      }
    }
    link.put("media_url", mediaUrlList.size() == 0 ? Collections.EMPTY_LIST : mediaUrlList);
    link.put("news_type", mediaUrlList.size() == 0 ? "txt" : hitSource.getNewsType());

  }


  public static void putCommon(JSONObject link, News hitSource) {
    link.put("id", hitSource.getId());
    if (hitSource.getNewsAuthor() != null)
      link.put("news_author",
          StringUtils.strip(hitSource.getNewsAuthor().toString().replace("[", "").replace("]", "")));
    else
      link.put("news_author", null);
    link.put("news_website", hitSource.getNewsWebsite());
    link.put("news_publictime_date", hitSource.getNewsPublicTime());
  }

  public static void putTitleContent(Map<String, List<String>> highlight,
                                     JSONObject link, News hitSource, boolean zh) {
    String zhStr = zh ? "_zh" : "";
    if (highlight != null) {
      if (highlight.get("news_title" + zhStr) != null) {
        List<String> highlightStrList = highlight.get("news_title" + zhStr);
        StringBuilder title = new StringBuilder();
        for (String str : highlightStrList) {
          title.append(str).append(">>>· · ·");
        }
        title.delete(title.length() - 6, title.length());
        link.put("news_title", title);
      } else {
        link.put("news_title", zh ? hitSource.getNewsTitleZh(): hitSource.getNewsTitle());
      }
      if (highlight.get("news_content" + zhStr) != null) {
        List<String> highlightStrList = highlight.get("news_content" + zhStr);
        StringBuilder content = new StringBuilder();
        for (String str : highlightStrList) {
          content.append(str).append(">>>· · ·");
        }
        content.delete(content.length() - 6, content.length());
        link.put("news_content", content);
      } else {
        link.put("news_content", zh ? hitSource.getNewsContentZh(): hitSource.getNewsContent());
      }
    } else {
      link.put("news_title", zh ? hitSource.getNewsTitleZh(): hitSource.getNewsTitle());
      link.put("news_content", zh ? hitSource.getNewsContentZh(): hitSource.getNewsContent());
    }
  }

  public static void putMaterialTitleContent(Map<String, List<String>> highlight,
                                     JSONObject link, JSONObject source) {
    if (highlight != null) {
      if (highlight.get("material_title") != null) {
        List<String> highlightStrList = highlight.get("material_title");
        StringBuilder title = new StringBuilder();
        for (String str : highlightStrList) {
          title.append(str).append(">>>· · ·");
        }
        title.delete(title.length() - 6, title.length());
        link.put("material_title", title);
      } else {
        link.put("material_title", source.get("material_title"));
      }
      if (highlight.get("material_content" ) != null) {
        List<String> highlightStrList = highlight.get("material_content");
        StringBuilder content = new StringBuilder();
        for (String str : highlightStrList) {
          content.append(str).append(">>>· · ·");
        }
        content.delete(content.length() - 6, content.length());
        link.put("material_content", content);
      } else {
        link.put("material_content", source.get("material_content"));
      }
    } else {
      link.put("material_title",  source.get("material_title"));
      link.put("material_content",  source.get("material_content"));
    }
  }


  public static void putMaterialTitleContent(Map<String, List<String>> highlight,
                                     JSONObject link, Material hitSource) {

    if (highlight != null) {
      if (highlight.get("material_title" ) != null) {
        List<String> highlightStrList = highlight.get("material_title" );
        StringBuilder title = new StringBuilder();
        for (String str : highlightStrList) {
          title.append(str).append(">>>· · ·");
        }
        title.delete(title.length() - 6, title.length());
        link.put("material_title", title);
      } else {
        link.put("material_title", hitSource.getMaterialTitle());
      }
      if (highlight.get("material_content" ) != null) {
        List<String> highlightStrList = highlight.get("material_content" );
        StringBuilder content = new StringBuilder();
        for (String str : highlightStrList) {
          content.append(str).append(">>>· · ·");
        }
        content.delete(content.length() - 6, content.length());
        link.put("material_content", content);
      } else {
        link.put("material_content", hitSource.getMaterialContent());
      }
    } else {
      link.put("material_title", hitSource.getMaterialTitle());
      link.put("material_content", hitSource.getMaterialContent());
    }
  }

  public static Set<String> wrappedKeyword(Object key) {
    Set<String> keywords = new LinkedHashSet<>();
    try {
      if (key != null) {
        if (key instanceof List) {
          List<Object> keyList = (List<Object>) key;
          if (!keyList.isEmpty()) {
            Object o = keyList.get(0);
            if (o instanceof String) {
              keywords.addAll((List)keyList);
            } else {
              List<String> collect = keyList.stream()
                  .map(x -> (String) ((Map) x).get("name"))
                  .collect(Collectors.toList());
              keywords.addAll(collect);
            }
          }
        } else {
          if (key instanceof Map) {
            keywords.add((String) ((Map) key).get("name"));
          } else {
            keywords.add((String) key);
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return keywords;
  }
}
